﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using shfa.snomtastic.admin.components;



namespace shfa.snomtastic.admin.controls
{
    public partial class ActionButtons : System.Web.UI.UserControl
    {

        public delegate void ActionEventHandler(object sender, ActionClickArgs e);

        public event ActionEventHandler ActionEvent;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void UpdateButtons(List<ActionInfo> ActionList)
        {
           
            pnlButtons.Controls.Clear();

            HtmlGenericControl ul = new HtmlGenericControl("ul");
            ul.Attributes.Add("class", "ActionButtons");
            
            foreach (ActionInfo ai in ActionList)
            {
                HtmlGenericControl li = new HtmlGenericControl("li");
                LinkButton lb = new LinkButton{ ID="lb" + ai.ActionName, CommandArgument=ai.ActionName };
                lb.Controls.Add(new Image { ID= "img" + ai.ActionName, ImageUrl=ai.ActionImage});
                lb.Controls.Add(new LiteralControl("<span>" + ai.ActionText + "</span>"));

                lb.Click += new EventHandler(lb_Click);
                lb.CausesValidation = ai.CausesValidation;
                if (!string.IsNullOrEmpty(ai.ActionClick))
                    lb.OnClientClick = "javascript:return(window.confirm('" + ai.ActionClick + "'))";

                li.Controls.Add(lb);
                ul.Controls.Add(li);
                if (!ai.ActionVisible)
                    li.Visible = false;
            }

            

            pnlButtons.Controls.Add(ul);

        
        }

        public void lb_Click(object sender, EventArgs e)
        {
            if (ActionEvent != null)
                ActionEvent(this, new ActionClickArgs(((LinkButton)sender).CommandArgument));

        }

    }
}